window.context = "/ga";
var pageSize=10;
var pageNumber=1;
var oprate=null;
var datatbody="datatbody";
var datathead="datathead";
$(function() {
	$("title").after("<link rel='stylesheet' type='text/css' href='" + window.context+ "/resources/css/style.css'>")
	$("title").after("<link type='image/x-icon' rel='icon' href='" + window.context+ "/ga.png' />")
	if($("input[op=datepicker]").html()!=undefined){
		$("input[op=datepicker]").datepicker({
		      changeMonth: true,
		      changeYear: true
		    });
		$("input[op=datepicker]").datepicker( "option", "dateFormat", "yy-mm-dd");
	}
	
	
});

$.fn.serializeJSON = function() {
	var o = {};
	var a = this.serializeArray();
	$.each(a, function() {
		if (o[this.name] !== undefined) {
			if (!o[this.name].push) {
				o[this.name] = [ o[this.name] ];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return o;
	// return JSON.stringify(o);
}
// 浏览器类型
function getOs() {
	var OsObject = "";
	if (navigator.userAgent.indexOf("MSIE") > 0) {
		return "MSIE";
	}
	if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) {
		return "Firefox";
	}
	if (isSafari = navigator.userAgent.indexOf("Safari") > 0) {
		return "Safari";
	}
	if (isCamino = navigator.userAgent.indexOf("Camino") > 0) {
		return "Camino";
	}
	if (isMozilla = navigator.userAgent.indexOf("Gecko/") > 0) {
		return "Gecko";
	}

}

function forminit(formId, json) {
	var a = $("#" + formId).serializeArray();
	$.each(a, function(i, item) {
		for ( var x in json) {
			if (x == item.name) {
				$("#" + x).val(json[x]);
			}
		}
	});
}
//text init
function textinit(json) {
	$("text").each(function(i,item){
		for ( var x in json) {
			if(x==item.id){
				$("#"+item.id).html(json[x]);
			}
		}
	});
}

function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return unescape(r[2]);
	return null;
}
function GetRestString() {
	var r = window.location.href;
	r = r.substr(r.lastIndexOf("/") + 1);
	return r;
}
function j2o(obj) {
	try {
		if (typeof (obj) == "object") {
			return obj;
		} else {
			return eval('(' + obj + ')');
		}
	} catch (e) {
		msg(e);
	}
}
//提示框
function msg(cotent,doc){
	if(!doc) doc=window.document;
	var h=$(doc).height();
	$(doc.body).append("<div id='msgmask' style='height:"+h+"px'></div>");
	$("#msgmask",doc).fadeIn(100,function(){
		var msg="<div id ='msg'>"+
							"<div class='mt' id='mt'>&nbsp;&nbsp;&nbsp;消 息 </div>"+
							"<div class='mc' id='mc'>&nbsp;&nbsp;&nbsp;"+cotent+"</div>"+
							"<div class='mo'><input type='button' class='button blue' value='关闭' onclick='closemsg();' >&nbsp;&nbsp;&nbsp;</div>"+
						"</div>";
		$(doc.body).append(msg);
		$("#msg",doc).css("left",window.screen.width/2-160);
		$("#msg",doc).css("top",window.screen.height/2-250);
		$("#mt",doc).css("cursor","move");
		$("#msg",doc).fadeIn(200);
		moveinit($("#mt",doc)[0],$("#msg",doc)[0],doc);
		document.onkeydown = function(e) {
			var ev = document.all ? window.event : e;
			if (ev.keyCode == 13||ev.keyCode == 27) {
				closemsg(doc);
			}
		}
	});
}
//confirm 确认框
function sure(cotent,func,doc,obj){
	if(obj!=undefined){
		$(obj).attr("disabled",true);
	}
	var h=$(doc).height();
	if(!doc) doc=window.document;
	$(doc.body).append("<div id='suremask' style='height:"+h+"px'></div>");
	$("#suremask",doc).fadeIn(200,function(){
		var msg="<div id ='sure'>"+
							"<div class='mt' id='mt'>&nbsp;&nbsp;&nbsp;消 息 </div>"+
							"<div class='mc' id='mc'>&nbsp;&nbsp;&nbsp;"+cotent+"</div>"+
							"<div class='mo'>" +
							"<input type='button' class='button blue' value='确认' id='ok' >&nbsp;&nbsp;&nbsp;" +
							"<input type='button' class='button blue' value='取消' id='cancel' />" +
							"&nbsp;&nbsp;&nbsp;</div>"+
						"</div>";
		$(doc.body).append(msg);
		$("#ok",doc).click(function(){
			func();
			closesure(doc);
			if(obj!=undefined){
				$(obj).attr("disabled",false);
			}
		});
		$("#cancel",doc).click(function(){
			closesure(doc);
			if(obj!=undefined){
				$(obj).attr("disabled",false);
			}
		});
		$("#sure",doc).css("left",window.screen.width/2-160);
		$("#sure",doc).css("top",window.screen.height/2-250);
		$("#mt",doc).css("cursor","move");
		$("#sure",doc).fadeIn(200);
		moveinit($("#mt",doc)[0],$("#sure",doc)[0],doc);
		document.onkeydown = function(e) {
			var ev = document.all ? window.event : e;
			if (ev.keyCode == 27) {
				closesure(doc);
			}
			if (ev.keyCode == 13) {
				$("#ok",doc).click();
			}
		}
	});
}
//关闭msg
function closemsg(doc){
	if(!doc) doc=window.document;
	$("#msg",doc).fadeOut(200,function(){
		$("#msgmask",doc).fadeOut(200);
		$("#msg",doc).remove();
		$("#msgmask",doc).remove();
	});
}
//关闭sure
function closesure(doc){
	if(!doc) doc=window.document;
	$("#sure",doc).fadeOut(200,function(){
		$("#suremask",doc).fadeOut(200);
		$("#sure",doc).remove();
		$("#suremask",doc).remove();
	});
}
//根据id关闭遮罩层
function closemid(id1,id2,doc,func){
	if(!doc) doc=window.document;
	$("#"+id1,doc).fadeOut(200,function(){
		$("#"+id2,doc).fadeOut(200);
		$("#"+id1,doc).remove();
		$("#"+id2,doc).remove();
	});
	if(func){
		func();
	}
}
//打开屏蔽层
function openmask(doc,func){
	var h=$(doc).height();
	if(!doc) doc=window.document;
	$(doc.body).append("<div id='mask' style='height:"+h+"px'></div>");
	$("#mask",doc).fadeIn(100,function(){
		if(func) func();
	});
}
// 动态增加ifame高度
function SetFrameHeight() {
	var sh=window.screen.height;
	var mh=$("#main").css("height");
	mh=mh.substring(0,mh.indexOf("px"));
	mh=parseInt(mh);
	$("#ifm", window.parent.document).attr("height", mh+10);
	if((mh+230)>sh){
		$("#foot", window.parent.document).css("top", mh+100);
	}else if(mh+230<sh){
		$("#foot", window.parent.document).css("top", sh-125);
	}else{
		$("#foot", window.parent.document).css("bottom", 10);
	}
	
//	var bs=document.body.scrollHeight;
//	alert("clientHeight=" + document.body.clientHeight);
//	alert("scrollHeight="+window.parent.document.body.scrollHeight);
//	alert("scrollHeight=" + document.body.scrollHeight);
//	alert("offsetHeight=" + document.body.offsetHeight);
//	alert("scrollHeight="+window.parent.document.body.scrollHeight);
//	alert("clientHeight="+window.parent.document.body.clientHeight);
//	alert("offsetHeight="+window.parent.document.body.offsetHeight);
	
}
//分页查询 
function search(datatbody,datathead){
	var op="";
	if(oprate!=null){
		$(oprate).each(function(i,item){
			op+="<input type='button' id='"+item.id+"' value='"+item.value+"' class='button "+item.color+"' onclick='"+item.func+"' />&nbsp;";
		});
	}
	var fulldata="";
	$("#"+datatbody).html("");
	var param=$("#paramForm").serializeJSON();
	param.pageNumber=$("#pageNumber").val();
	param.pageSize=$("#pageSize").val();
	$.post(requestUrl,param,function(data){
		$("#totalPage").text(data.totalPage);
		$("#totalRow").text(data.totalRow);
		$(data.list).each(function(i,item){
			var tddata="";
			$("#"+datathead+" th").each(function(i,ite){
				var name=$(ite).attr("name");
				var bz=$(ite).attr("bz");
				if(name=="oprate"){
					tddata+="<td><id>"+item.id+"</id>"+op+"</td>";
				}else if(bz=="pic"){
					var w=30;
					var h=30;
					if($(ite).attr("w")) w=$(ite).attr("w");
					if($(ite).attr("h")) h=$(ite).attr("h");
					tddata+="<td><id>"+item.id+"</id><a href='javascript:void(0)' onclick='detail(this)' >" +
									"<img style='border:0' src='../../"+item[name]+"' width='"+w+"px' height='"+h+"px' />" +
									"</a></td>";
				}else if(bz=="title"){
					tddata+="<td><id>"+item.id+"</id><a href='javascript:void(0);' onclick='detail(this)'>"+item[name]+"</a></td>";
				}else{
					tddata+="<td>"+item[name]+"</td>";
				}
			})
			//拼凑数据
		    fulldata+="<tr>"+tddata+"</tr>";
		});
		$("#"+datatbody).html(fulldata);
		//动态设置父窗口iframe高度
		
		$("#"+datatbody+" tr:odd").css("background-color", "#FAFAFA"); 
		$("#"+datatbody+" tr:even").css("background-color","#FEFEFE"); 
		
		var pagebar=
		"<input type='button' value='首页' class='button greyish' onclick='first()' />"+
		"<input type='button' value='上一页' class='button greyish' onclick='pre()' />"+
		"每页 :"+
			"<select id='pageSize'>"+
				"<option value='10' "+pageSizedefault(10,data.pageSize)+">10</option>"+
				"<option value='20'"+pageSizedefault(20,data.pageSize)+">20</option>"+
				"<option value='50'"+pageSizedefault(50,data.pageSize)+">50</option>"+
				"<option value='100'"+pageSizedefault(100,data.pageSize)+">100</option>"+
			"</select>"+
		"条 "+
		"当前第 :<input type='text' id='pageNumber' value='"+data.pageNumber+"' />页 "+
		"<input type='button' value='下一页' class='button greyish' onclick='next()' />"+
		"<input type='button' value='尾页' class='button greyish' onclick='last()' /> "+
		"总共 <span id='totalPage'>"+data.totalPage+"</span> 页 "+
		"总共 <span id='totalRow'>"+data.totalRow+"</span> 条记录";
		$("div[bar=pagebar]").html(pagebar);
		SetFrameHeight();
		$("#pageSize").change(function(){
			search(datatbody,datathead);
		})
		$("input[type=reset]").click(function(){
			$("#pageNumber").val("1");
		})
	});
}
function pageSizedefault(num,pageSize){
	if(num==pageSize) return "selected=true";
}
//首页
function first(){
	$("#pageNumber").val(1);
	search(datatbody,datathead);
}
//上一页
function pre(){
	var pn=$("#pageNumber").val();
	if(pn!=1&&pn!=""&&pn!=null&&pn!=undefined){
		$("#pageNumber").val(parseInt(pn)-1);
		search(datatbody,datathead);
	}
}
//下一页
function next(){
	var pn=$("#pageNumber").val();
	if(pn!=""&&pn!=null&&pn!=undefined&&pn!=$("#totalPage").text()){
		$("#pageNumber").val(parseInt(pn)+1);
		search(datatbody,datathead);
	}
}
//尾页
function last(){
	var pn=$("#totalPage").text();
		$("#pageNumber").val(pn);
		search(datatbody,datathead);
}

//进度条
var x=0;
function progress(func){
	$("body").append("<div id='cover'></div>");
	$("body").append("<div id='progressbar'><div id='progress'></div></div>");
	$("#progressbar").css("left",window.screen.width/2-400);
	$("#progressbar").css("top",window.screen.height/2-250);
	$("#cover").fadeIn(200,function(){
		$("#progress").css("width","0px");
		$("#progress").css("text-align","center");
		$("#progress").html("0%");
		progressPercentage();
		$("#progress").animate({width:"600px"},2000,function(){
			func(function(f){
				$("#progress").html("76%");
				progressPercentage();
				$("#progress").animate({width:"800px"},500,function(){
					$("#cover").remove();
					$("#progressbar").remove();
					f();
				});
			});
		
		});
	});
	
}
//进度条百分比
function progressPercentage(){
	var p=$("#progress").html();
	if(p!=undefined){
		p=p.substring(0,p.indexOf("%"));
		p=parseInt(p);
		p=p+1;
		$("#progress").html(p+"%");
		if(p<75||p>=76&&p!=100){
			setTimeout(progressPercentage,25);
		}
	}
}
//拖动
function moveinit(tdiv,mdiv,doc){
	var dargX = 0;
	var dargY = 0;
	tdiv.onmousedown = function(ev){
		var oEvent = ev || event;
		dargX = oEvent.clientX - mdiv.offsetLeft;//获取鼠标与div左边的距离
		dargY = oEvent.clientY - mdiv.offsetTop;//获取鼠标与div头部的距离
		mdiv.onmousemove = function(ev){
			var oEvent = ev||event;
			var Left = oEvent.clientX - dargX;
			var Top = oEvent.clientY - dargY;
			if (Left<0){
				Left=0;
			}
			if(Left>doc.documentElement.clientWidth-mdiv.offsetWidth){
				Left = doc.documentElement.clientWidth-mdiv.offsetWidth;
			}
			if (Top<0){
				Top=0;
			}
			if (Top>doc.documentElement.clientHeight-mdiv.offsetHeight){
				Top = doc.documentElement.clientHeight-mdiv.offsetHeight;
			}
			mdiv.style.left = Left + "px";
			mdiv.style.top = Top + "px"; 
		}
			mdiv.onmouseup = function(){
			mdiv.onmousemove = null;
			mdiv.onmouseup = null;
		};
		return false;
	}
}
//detail update common method
function du(detailurl,opid,obj,funcinit,funcsave,w){
	$(obj).attr("disabled",true);
	var id=$(obj).parent().find("id").html();
	$.post(detailurl,{id:id},function(data){
		funcinit(data);
		$("#"+opid).css("left",window.screen.width/2-w);
		$("#"+opid).css("top",95);
		var wpd=window.parent.document;
		var opo=$("#"+opid).clone();
		$(wpd.body).append(opo);
		//返回事件
		$("#back",wpd).click(function(){
			closemid(opid,"mask",wpd);
			$(obj).attr("disabled",false);
		});
		//保存 审核 事件
		$("#save,#pass,#nopass",wpd).click(function(){
			funcsave(opo,this,function(flag){
				closemid(opid,"mask",wpd,function(){
					if(flag==1){
						msg("操作成功！",wpd);
						$(obj).attr("disabled",false);
					}else{
						msg("操作失败！",wpd);
						$(obj).attr("disabled",false);
					}
					search(datatbody,datathead);
				});
			});
		});
		//radio 点击事件
		$("input[type=radio]",wpd).click(function(){
			$("input[type=radio]",wpd).attr("c",false);
			$(this,wpd).attr("c",true);
		});
		//file change 事件
		$("input[type=file]",wpd).change(function(){
			var obj=this;
			 $("#fileupload",wpd).ajaxSubmit({
				 	type:"POST",
				 	url:fileChangeUrl,
				 	error:function(){msg("上传图片失败了！",wpd)},
		        	success:function(data){
		        		if(data.imgsrc&&data.imgsrc!="") {
		        			var img=$(obj).parent().parent().find("img");
		        			img.attr("src",window.context+data.imgsrc);
		        		}else if(data.poster&&data.poster!=""){
		        			$(obj).parent().parent().find("video").attr("poster",'../'+data.poster);
		        		}
		        	}
		        });
		});
		
		
		var mdiv=$("#"+opid,wpd);
		var odiv=mdiv.find("#mtitle");
		moveinit(odiv[0],mdiv[0],window.parent.document);
		openmask(wpd,function(){
			$("#"+opid,wpd).fadeIn(200);
			//注入键盘按下事件
			document.onkeydown = function(e) {
				var ev = document.all ? window.event : e;
					if (opid=="detail"&&(ev.keyCode == 27||ev.keyCode == 13)) {
						closemid(opid,"mask",wpd);
						$(obj).attr("disabled",false);
					}else if(opid=="update"&&ev.keyCode==27){
						closemid(opid,"mask",wpd);
						$(obj).attr("disabled",false);
					}else if(opid=="update"&&ev.keyCode==13){
						$("#save",wpd).click();
					}
			}
		});
	})
}





